package function;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import com.alibaba.fastjson.JSON;

import form.CommonForm;
import form.WindowsMsg;
import main.Frame;

public class IO {

	private final File f = new File("xy.xycf");

	public boolean save(CommonForm cf, WindowsMsg win1, WindowsMsg win2,
			WindowsMsg win3, WindowsMsg win4, WindowsMsg win5,String function) {
		StringBuffer str = new StringBuffer();
		str.append(cf.getArea());
		str.append("'");
		str.append(cf.getServer());
		str.append("'");
		str.append(win1.getName());
		str.append("'");
		str.append(win1.getID());
		str.append("'");
		str.append(win2.getName());
		str.append("'");
		str.append(win2.getID());
		str.append("'");
		str.append(win3.getName());
		str.append("'");
		str.append(win3.getID());
		str.append("'");
		str.append(win4.getName());
		str.append("'");
		str.append(win4.getID());
		str.append("'");
		str.append(win5.getName());
		str.append("'");
		str.append(win5.getID());
		str.append("'");
		FileOutputStream fos = null;
		OutputStreamWriter osw = null;
		BufferedWriter bw  =null;
		try {
			fos = new FileOutputStream(f);
			osw = new OutputStreamWriter(fos);
			bw = new BufferedWriter(osw);
			bw.write(str.toString()+function);
		} catch (FileNotFoundException e) {
		} catch (IOException e) {
		}finally{
			try {
				bw.flush();
				bw.close();
				osw.close();
				fos.close();
			} catch (IOException e) {
			}
		}
		return false;
	}
	public boolean save() {
		FileOutputStream fos = null;
		OutputStreamWriter osw = null;
		BufferedWriter bw  =null;
		try {
			fos = new FileOutputStream(f);
			osw = new OutputStreamWriter(fos);
			bw = new BufferedWriter(osw);
			bw.write(JSON.toJSONString(Frame.storage));
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				bw.flush();
				bw.close();
				osw.close();
				fos.close();
			} catch (IOException e) {
			}
		}
		return false;
	}
	public String read() {
		FileInputStream fis = null;
		InputStreamReader isr = null;
		BufferedReader br = null;
		if (f.exists()) {
			try {
				fis = new FileInputStream(f);
				isr = new InputStreamReader(fis);
				br = new BufferedReader(isr);
				return br.readLine();
			} catch (FileNotFoundException e) {
			} catch (IOException e) {
			} finally {
				try {
					br.close();
					isr.close();
					fis.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			System.out.println("error~!");
		}
		return null;
	}
}
